package com.avocado.base.controller;


import com.avocado.base.dto.organization.OrganizationAddDto;
import com.avocado.base.dto.organization.OrganizationEditDto;
import com.avocado.base.service.OrganizationService;
import com.avocado.commons.model.http.BaseResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;




@Controller
@RequestMapping("/organization/*")
public class OrganizationController {
	

	private final OrganizationService organizationService;

	public OrganizationController(OrganizationService organizationService) {
		this.organizationService = organizationService;
	}

	 @PreAuthorize("hasAnyAuthority('ORGANIZATION_ADD')")
	 @RequestMapping("/add")
     public @ResponseBody BaseResponse add(@RequestBody @Validated OrganizationAddDto t){

		long id=organizationService.add(t);

		return BaseResponse.builder().status(1).message("操作成功!").data(id).build();
    }

	 @PreAuthorize("hasAnyAuthority('ORGANIZATION_UPDATE')")
	 @RequestMapping("/update")
     public @ResponseBody BaseResponse update(@RequestBody @Validated OrganizationEditDto t){

		 organizationService.update(t);

		 return BaseResponse.builder().status(1).message("操作成功!").build();
     }


	 @PreAuthorize("hasAnyAuthority('ORGANIZATION_DELETE')")
	 @RequestMapping("/deleteById")
     public @ResponseBody BaseResponse deleteById(Long id) {

		 int status=organizationService.deleteById(id)==Boolean.TRUE?1:0;

		 return BaseResponse.builder().status(status).message("操作成功!").build();

     }
	 
	 @RequestMapping("/getTree")
     public @ResponseBody BaseResponse getTree() {

		 return BaseResponse.builder().status(1).data(organizationService.getTree()).build();

     }
	 
	
	
	

}
